package com.theatre.userservice.Controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.theatre.userservice.annotation.RequirePermission;
import com.theatre.userservice.Controller.contxet.BaseContext;
import com.theatre.userservice.mapper.PublisherMapper;
import com.theatre.userservice.mapper.ScriptMapper;
import com.theatre.userservice.model.dto.ScriptDTO;
import com.theatre.userservice.model.entity.Publisher;
import com.theatre.userservice.model.entity.Script;
import com.theatre.userservice.model.vo.Result;
import com.theatre.userservice.model.vo.utils.FileUploadUtil;
import com.theatre.userservice.service.ScriptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;

@RestController
@RequestMapping("/script")
@Tag(name = "剧本接口", description = "提供剧本相关的接口")
@Slf4j
public class ScriptController {
    @Autowired
    private ScriptService scriptService;
    @Autowired
    private ScriptMapper scriptMapper;
    @Autowired
    private PublisherMapper publisherMapper;

    @Value("${upload.base-dir}")
    private String uploadBaseDir;

    @RequirePermission("content:manage")
    @PostMapping("/commit")
    @Operation(summary = "上传剧本", description = "上传剧本")
    public Result<String> commitScript(@RequestBody ScriptDTO scriptDTO) {
        //获取当前用户id
        Long currentUserId = BaseContext.getUserId();
        Script script = new Script();
        BeanUtils.copyProperties(scriptDTO, script);
        Long publisherId = scriptDTO.getPublisherId();
        if (publisherId == null) {
            return Result.error("缺少发行商ID");
        }
        Publisher publisher = publisherMapper.selectById(publisherId);
        if (publisher == null) {
            return Result.error("发行商不存在");
        }
        script.setPublishName(publisher.getName());
        // 2. 发布日期只取年月日
        script.setPublishDate(LocalDate.now().toString());
        script.setUsageDays(30);
        script.setStatus(0);
        script.setSold(0);
        scriptService.save(script);
        return Result.success("commit");
    }
    @RequirePermission("content:manage")
    @PostMapping("/upload")
    @Operation(summary = "上传剧本图片", description = "上传剧本图片")
    public Result<String> uploadScript(@RequestParam("id") Long id,
                                       @RequestParam("file") MultipartFile file) {
        Long currentUserId = BaseContext.getUserId();

        // 简单权限判断
        if (currentUserId == null || currentUserId == 3 || currentUserId == 4) {
            return Result.error("没有资格上传剧本");
        }

        if (file == null || file.isEmpty()) {
            return Result.error("请上传剧本图片文件");
        }

        try {
            // 保存文件
            String relativePath = FileUploadUtil.saveFile(file, uploadBaseDir, "script");

            // 更新数据库
            Script script = new Script();
            script.setId(id);
            script.setContentFile(relativePath);
            scriptMapper.updateById(script);

            return Result.success(relativePath);
        } catch (Exception e) {
            throw new RuntimeException("上传剧本图片失败", e);
        }
    }
}
